package com.km.sglototo.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.km.sglototo.annotation.SysLog;
import com.km.sglototo.sys.entity.TbInterview;
import com.km.sglototo.sys.service.TbInterviewService;
import com.km.sglototo.utils.CommonResult;
import com.km.sglototo.utils.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xt
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/interview")
public class TbInterviewController {
    @Resource
    private TbInterviewService tbInterviewService;

    @SysLog(MODULE = "interview", REMARK = "面试查询")
    @ApiOperation("面试查询")
    @GetMapping(value = "/list")
    public Object getList(TbInterview entity,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<TbInterview> queryWrapper = new QueryWrapper<TbInterview>();
        if (entity.getName() != null) {
            queryWrapper.like("name", entity.getName());
        } else if (entity.getResult() != null) {
            queryWrapper.like("result", entity.getResult());
        }
        Page<TbInterview> p = new Page<TbInterview>(pageNum, pageSize);
        return new CommonResult().success(tbInterviewService.page(p, queryWrapper));

    }


    @SysLog(MODULE = "interview_add", REMARK = "新增面试")
    @ApiOperation("新增面试")
    @PostMapping(value = "/add")
    public Object addTbInterview(@RequestBody TbInterview entity) {
        boolean b = false;
        if (entity.getInfoId() != null) {
            b = tbInterviewService.updateById(entity);
        } else {
            b = tbInterviewService.save(entity);
        }
        return new CommonResult().success(b);
    }


    @SysLog(MODULE = "Updata", REMARK = "更新面试信息")
    @ApiOperation("更新面试信息")
    @PostMapping(value = "/update/{id}")
    public Object updateInterview(@RequestBody TbInterview entity) {
        boolean b = tbInterviewService.updateById(entity);
        return new CommonResult().success();
    }

    @SysLog(MODULE = "interview_delete", REMARK = "删除面试信息")
    @ApiOperation("删除面试信息")
    @GetMapping(value = "/delete/{id}")
    public Object deleteInterview(@ApiParam("用户id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("用户id");
            }

            QueryWrapper<TbInterview> qw = new QueryWrapper<>();
            qw.eq("info_id", id);
            if (tbInterviewService.remove(qw)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            // log.error("删除用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }


    @SysLog(MODULE = "interview_deletes", REMARK = "批量删除面试信息")
    @ApiOperation("批量删除面试信息")
    @GetMapping(value = "deleteBatch")
    public Object deleteInterviewBatch(Integer[] ids) {
        List<Integer> list = new ArrayList<>();
        for (Integer id : ids) {
            list.add(id);
        }
        if (tbInterviewService.removeByIds(list)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed();
    }


    @SysLog(MODULE = "interview_deletes", REMARK = "Post批量删除面试信息")
    @ApiOperation("Post批量删除面试信息")
    @PostMapping("/deletePost")
    public Object deletePost(@RequestBody List<Integer> ids) {
        List<Integer> list = new ArrayList<>();
        for (Integer id : ids) {
            list.add(id);
        }
        if (tbInterviewService.removeByIds(list)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed();
    }

    @PostMapping("/deleteBatchPost2")
    public Object deleteBatchPost2(@RequestBody List<TbInterview> ids) {
        System.out.println(ids);
        List<Integer> list = new ArrayList<>();
        for (TbInterview tbInterview : ids) {
            list.add(tbInterview.getInfoId());
        }
        boolean is_succ = tbInterviewService.removeByIds(list);
        return new CommonResult().success(is_succ);
    }


    @SysLog(MODULE = "get_interview ", REMARK = "根据id获取面试信息表")
    @ApiOperation("根据id获取面试信息表")
    @GetMapping(value = "/getById/{id}")
    public Object getById(@ApiParam("interview_id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("id");
            }
            TbInterview obj = null;
            if (id != null) {
                obj = tbInterviewService.getById(id);
                return new CommonResult().success(obj);
            }

        } catch (Exception e) {
            // log.error("删除用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }


}

